/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package my.cbr.similarity;

import java.util.List;
import jcolibri.exception.NoApplicableSimilarityFunctionException;
import jcolibri.method.retrieve.NNretrieval.similarity.LocalSimilarityFunction;

/**
 *
 * @author Geir Ole
 */
public class ListInterval implements LocalSimilarityFunction {

    public double interval;

    public ListInterval(double interval) {
        this.interval = interval;
    }
    
    public double compute(Object caseObject, Object queryObject) throws NoApplicableSimilarityFunctionException {
        List caseO;
        List queryO;
        
        if(caseObject==null || queryObject==null || interval==0)
            return 0.0;
        else
        {
            try {
                caseO = (List) caseObject;
                queryO = (List) queryObject;
            } catch (Exception e) {
                return 0.0;
            }
        }
        int v1 = caseO.size();
        int v2 = queryO.size();
        return 1 - ((double) Math.abs(v1 - v2) / this.interval);
    }

    public boolean isApplicable(Object caseObject, Object queryObject) {
        if(caseObject==null || queryObject==null)
            return false;
        if(!(caseObject instanceof List && queryObject instanceof List))
            return false;
        return true;
    }
}
